/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 小梅就是酱子
 * Date: 2022-02-04
 * Time: 16:22
 */
public class 二分查找 {
    public static void main(String[] args) {
        int[] array={1,2,4,7,9};
        System.out.println(find(array,4));
    }
    public static int find(int[] array,int i){
        int left=0;
        int right= array.length-1;
        while(left<=right){
            int mid=(left+right)/2;
            if(array[mid]<i){
                right=mid-1;
            }
            else if(array[mid]>i){
                left=mid+1;
            }
            else{
                return mid;
            }
        }
        return -1;
    }
}
